from django.db import models

# Create your models here.


# 管理器是Django的模型进行数据库操作的接口
# 自定义管理器方法
class BookInfoManager(models.Manager):
    # 在终端中执行：python manage.py shell
    # from booktest.models import BookInfo
    # BookInfo.books.create_new_book('聊斋', '1999-01-01')
    def create_new_book(self, title, date):
        BookInfo.books.create(btitle=title, bpub_date=date)


class BookInfo(models.Model):
    """
    定义书籍模型类
    创建模型类对象的例子：
    book = BookInfo(
    btitle='西游记',
    bput_date=date(1988,1,1),
    bread=10,
    bcomment=10
)
    """
    # verbose_name 更改显示的名字,前端页面上显示
    # btitle 在数据库中显示的book对象名称
    btitle = models.CharField(max_length=20, verbose_name='名称')
    bpub_date = models.DateField(verbose_name='发布日期')
    bread = models.IntegerField(default=0, verbose_name='阅读量')
    bcomment = models.IntegerField(default=0, verbose_name='评论量')
    is_delete = models.BooleanField(default=False, verbose_name='逻辑删除')

    # 自定义模型管理器
    books = BookInfoManager()

    # 元选项
    class Meta:
        db_table = 'tb_books'  # 指明数据库表名,可以更改原表名
        verbose_name = '图书'  # 在admin站点中显示的名称
        verbose_name_plural = verbose_name  # 显示的复数名称

    def __str__(self):
        """定义每个数据对象的显示信息"""
        return self.btitle


# 定义英雄模型类HeroInfo
class HeroInfo(models.Model):
    GENDER_CHOICES = (
        (0, 'male'),
        (1, 'female')
    )
    hname = models.CharField(max_length=20, verbose_name='名称')
    hgender = models.SmallIntegerField(choices=GENDER_CHOICES, default=0, verbose_name='性别')
    hcomment = models.CharField(max_length=200, null=True, verbose_name='描述信息')
    hbook = models.ForeignKey(BookInfo, on_delete=models.CASCADE, verbose_name='图书')  # 外键
    is_delete = models.BooleanField(default=False, verbose_name='逻辑删除')

    class Meta:
        db_table = 'tb_heros'
        verbose_name = '英雄'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.hname